package com.imdroid.carac.activities;

import android.content.Context;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.graphics.drawable.AnimationDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;

import com.imdroid.carac.IMDroid;
import com.imdroid.carac.R;

public class FloatView {

	/**
	 * The float view
	 */
	private static View floatView;
	/**
	 * The surface of the camera
	 */
	static SurfaceView surfaceView;
	/**
	 * The windows layout parames
	 */
	private static WindowManager.LayoutParams params;

	/**
	 * Initialize the view
	 */
	@SuppressWarnings("deprecation")
	public static void init() {
		// 生成浮动窗
		floatView = LayoutInflater.from(IMDroid.getContext()).inflate(
				R.layout.float_view, null);
		surfaceView = ((SurfaceView) floatView
				.findViewById(R.id.floatSurfaceView));

		// 浮动窗的触摸事件：拖动，点击
		floatView.setOnTouchListener(new View.OnTouchListener() {
			// the point of press
			Point down;
			// If it is moving
			boolean moved;

			public boolean onTouch(View v, MotionEvent event) {
				int x = (int) event.getRawX();
				int y = (int) event.getRawY();
				// 按下事件
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					down = new Point(x, y);
					moved = false;

				}// 移动事件
				else if (event.getAction() == MotionEvent.ACTION_MOVE) {
					if (Math.abs(x - down.x) < 20 && Math.abs(y - down.y) < 20)
						return true;
					params.x = x - floatView.getWidth() / 2;
					params.y = y - floatView.getHeight() / 2; // TODO
																// 这样在全屏下正常，但应该考虑状态栏。
					WindowManager windowManager = (WindowManager) IMDroid
							.getContext().getSystemService(
									Context.WINDOW_SERVICE);
					windowManager.updateViewLayout(floatView, params);
					moved = true;

				}// 挪开事件
				else if (event.getAction() == MotionEvent.ACTION_UP) {
					if (moved)
						return true;
					if (Record.camera == null)
						Record.start();
					else
						Record.stop();
				}
				return true;
			}
		});

		// 显示位置
		params = new WindowManager.LayoutParams(
				WindowManager.LayoutParams.WRAP_CONTENT,
				WindowManager.LayoutParams.WRAP_CONTENT, 0, 0,
				WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
				WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
						| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
				PixelFormat.TRANSPARENT);
		params.alpha = 0;
		params.gravity = Gravity.LEFT | Gravity.TOP;
		WindowManager windowManager = (WindowManager) IMDroid.getContext()
				.getSystemService(Context.WINDOW_SERVICE);
		int w = windowManager.getDefaultDisplay().getWidth();
		int h = windowManager.getDefaultDisplay().getHeight();
		params.x = -w / 2;
		params.y = h * 5 / 8;
		windowManager.addView(floatView, params);
	}

	// 释放浮动窗
	public static void finish() {
		stop();
		WindowManager windowManager = (WindowManager) IMDroid.getContext()
				.getSystemService(Context.WINDOW_SERVICE);
		windowManager.removeView(floatView);
		params = null;
		surfaceView = null;
		floatView = null;
	}

	// 显示浮动窗：不透明，接收触摸事件
	public static void show() {
		params.alpha = 1;
		params.flags &= ~WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
		WindowManager windowManager = (WindowManager) IMDroid.getContext()
				.getSystemService(Context.WINDOW_SERVICE);
		windowManager.updateViewLayout(floatView, params);
	}

	// 隐藏浮动窗：变透明，不接收触摸事件
	public static void hide() {
		stop();
		params.alpha = 0;
		params.flags |= WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
		WindowManager windowManager = (WindowManager) IMDroid.getContext()
				.getSystemService(Context.WINDOW_SERVICE);
		windowManager.updateViewLayout(floatView, params);
	}

	// 开始动画
	public static void start() {
		((AnimationDrawable) floatView.findViewById(R.id.recordAnimate)
				.getBackground()).start();
	}

	// 停止动画
	static void stop() {
		((AnimationDrawable) floatView.findViewById(R.id.recordAnimate)
				.getBackground()).stop(); // 停止时不知道是第几帧
		((AnimationDrawable) floatView.findViewById(R.id.recordAnimate)
				.getBackground()).start(); // 现在是第一帧
		((AnimationDrawable) floatView.findViewById(R.id.recordAnimate)
				.getBackground()).stop(); // 停在第一帧
	}

}
